package com.leetcode.offer.chapter5;

/**
 * @author Dennis Li
 * @date 2020/7/17 10:16
 */
public class CountDigitOne_43 {

    // 找规律的题，没做好
    public int countDigitOne(int n) {
        int digit = 1, res = 0;
        int high = n / 10, cur = n % 10, low = 0;
        while(high != 0 || cur != 0) {
            // 高位 * 低位为1的个数
            // 然后通过对当前位进行判断，找真实的为1的个数
            if(cur == 0) res += high * digit;
            else if(cur == 1) res += high * digit + low + 1;
            else res += (high + 1) * digit;
            // 看低位有多少种为1的方式
            low += cur * digit;
            cur = high % 10;
            high /= 10;
            digit *= 10;
        }
        return res;
    }

}
